// Copyright (c) 2007 Robin Boerdijk. All rights reserved.
//
// This software is licensed under the terms of the Microsoft Reciprocal License as described 
// in the file LICENSE.txt, which you should have received as part of this distribution. The 
// license terms are also available at http://www.opensource.org/licenses/ms-rl.html.

#include "wtkpc.h"

#include "wtk/ContentBox.h"

namespace wtk
{
	ContentBox::ContentBox()
		: m_margin(Zero<RECT>())
		, m_cachedMinimumSize(Invalid<SIZE>())
		, m_cachedHorizontalElasticity(-1)
		, m_cachedVerticalElasticity(-1)
	{
	}

	void ContentBox::SetMargin(const RECT &margin)
	{
		m_margin = margin;
	}

	void ContentBox::SetMargin(long left, long top, long right, long bottom)
	{
		Set(m_margin, left, top, right, bottom);
	}

	void ContentBox::SetMargin(long leftRight, long topBottom)
	{
		Set(m_margin, leftRight, topBottom);
	}

	void ContentBox::SetMargin(long margin)
	{
		Set(m_margin, margin);
	}

	SIZE ContentBox::GetMinimumSize()
	{
		if (IsInvalid(m_cachedMinimumSize)) {
			m_cachedMinimumSize = CalculateMinimumContentSize();
			m_cachedMinimumSize.cx += m_margin.left + m_margin.right;
			m_cachedMinimumSize.cy += m_margin.top + m_margin.bottom;
		}
		return m_cachedMinimumSize;
	}

	long ContentBox::GetHorizontalElasticity()
	{
		if (m_cachedHorizontalElasticity < 0) {
			m_cachedHorizontalElasticity = CalculateHorizontalElasticity();
		}
		return m_cachedHorizontalElasticity;
	}

	long ContentBox::GetVerticalElasticity()
	{
		if (m_cachedVerticalElasticity < 0) {
			m_cachedVerticalElasticity = CalculateVerticalElasticity();
		}
		return m_cachedVerticalElasticity;
	}

	void ContentBox::Layout(const SIZE &size, const POINT &position)
	{
		LayoutContent(Construct<POINT>(position.x + m_margin.left, position.y + m_margin.top), 
			Construct<SIZE>(size.cx - m_margin.left - m_margin.right, size.cy - m_margin.top - m_margin.bottom));
	}
}
